//
//  dayGanttView.m
//  planningV3
//
//  Created by richard_local on 26/09/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "dayGanttView.h"

@interface enteteView : UIView
@end
@implementation enteteView
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    NSLog(@"DELEGATE LAYER ENTETE");
    // [super drawLayer:layer inContext:ctx];
    UIGraphicsPushContext(ctx);
    //[self setBackgroundColor:[UIColor whiteColor]];
    [[UIColor whiteColor] set];
    UIRectFill(layer.bounds);
    
    //Balise
    
   // CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
   // CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
     CGContextSetRGBFillColor(ctx, 0.0, 0, 0, 1.0);
     CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0);
    CGContextSelectFont(ctx, "Helvetica", 15, kCGEncodingMacRoman);
   // CGContextSetTextMatrix(ctx, CGAffineTransformMakeRotation(M_PI/2));
    CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(ctx, transform);
    CGContextSetTextDrawingMode(ctx, kCGTextFill);
    for(int i=0; i < 24; i ++ ){
        NSString *t = [NSString stringWithFormat:@"%d",cadran[i]];
        const char *str=[t UTF8String];
        CGContextShowTextAtPoint(ctx,LARGEUR_COLONNE+(LARGEUR_HEURE*i)-5,30.0,str,strlen(str));
        
        
        //draw line 
        //Set the stroke (pen) color
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        //Set the width of the pen mark
        CGContextSetLineWidth(ctx, 1.0);
        // Draw a line
        //Start at this point
        CGContextMoveToPoint(ctx, LARGEUR_COLONNE+(LARGEUR_HEURE*i), 35.0);
        
        //Give instructions to the CGContext
        //(move "pen" around the screen)
        CGContextAddLineToPoint(ctx,LARGEUR_COLONNE+(LARGEUR_HEURE*i),HAUTEUR_ENTETE);
        CGContextStrokePath(ctx);


    }
    //
    
    
    UIGraphicsPopContext();
    
    
    
}
@end
/* ganttBandeau *bd = [[ganttBandeau alloc] initWithFrame:CGRectMake(0.0f, HAUTEUR_ENTETE,LARGEUR_COLONNE-5,self.bounds.size.height )];
ganttBandeau *bd = [[ganttBandeau alloc]
                    initWithDateDeb:[listeHoraire objectAtIndex:(j*4)+2]
                    DateFin:[listeHoraire objectAtIndex:(j*4)+3]
                    ligne:[listeHoraire objectAtIndex:(j*4)]
                    type:[listeHoraire objectAtIndex:(j*4)+1]*/
@interface ganttBandeau : UIView
-(id)initWithDateDeb:(NSString*)dateDeb DateFin:(NSString*)datefin ligne:(int) ligne type:(int)type;

@end
@implementation ganttBandeau
-(id)initWithDateDeb:(NSString*)dateDeb DateFin:(NSString*)datefin ligne:(int) ligne type:(int)type
{
    int r = rand() %300;
    NSLog (@"********** \n\n %d",r);
    if ((self = [super initWithFrame:CGRectMake(1500+r, (HAUTEUR_LIGNE*(ligne))+HAUTEUR_ENTETE+HAUTEUR_LIGNE/2, 200, 10)])) {
        // your init stuff here
        self.backgroundColor = [UIColor redColor];
        if (r > 120) {
        self.backgroundColor = [UIColor blueColor];
        }
        
        self.alpha = 0.7;
    }
    return self;

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//vor moveme sur appledev 
}

@end
@interface bandeauGView : UIView

@end

@implementation bandeauGView
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    NSArray *listeE=[NSArray arrayWithObjects:@"luigi",@"jean",@"leon",@" mario",nil];
    UIGraphicsPushContext(ctx);

    [[UIColor whiteColor] set];
    UIRectFill(layer.bounds);
    [[UIColor blackColor] set];
    
    CGContextSetRGBFillColor(ctx, 0.0, 0, 0, 1.0);
    CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0);
    CGContextSelectFont(ctx, "Helvetica", 15, kCGEncodingMacRoman);
    CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(ctx, transform);
    CGContextSetTextDrawingMode(ctx, kCGTextFill);
    
    //ligne en haut
    CGContextMoveToPoint(ctx, 0,2);
    
    //Give instructions to the CGContext
    //(move "pen" around the screen)
    CGContextAddLineToPoint(ctx,LARGEUR_COLONNE,2);
    CGContextStrokePath(ctx);

    
    
    for(int i=0; i < [listeE count]; i ++ ){
        NSString *t = [listeE objectAtIndex:i];
        const char *str=[t UTF8String];
        CGContextShowTextAtPoint(ctx,10,(HAUTEUR_LIGNE*(i-1))+HAUTEUR_ENTETE+HAUTEUR_LIGNE/2,str,strlen(str));
        
        //ligne en haut
        CGContextMoveToPoint(ctx, 0,(HAUTEUR_LIGNE*i)+HAUTEUR_ENTETE);
        
        //Give instructions to the CGContext
        //(move "pen" around the screen)
        CGContextAddLineToPoint(ctx,LARGEUR_COLONNE,(HAUTEUR_LIGNE*i)+HAUTEUR_ENTETE);
        CGContextStrokePath(ctx);

        
        //ligne en bas
        
    }
    
    
    UIGraphicsPopContext();

}
@end
@implementation dayGanttView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _coinView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, LARGEUR_COLONNE-5, HAUTEUR_ENTETE)];
        _coinView.backgroundColor = [UIColor whiteColor];
        [_coinView setAlpha:0.6];
        
        
        _listeHoraire = [self getlisteHoraire];
        _listeEmploye= [self getlisteEmploye];
        [_listeHoraire.layer setNeedsDisplay];
        [_listeEmploye.layer setNeedsDisplay];
       
      
        
        
        
        // ajout des vues de gantt
        // ici pas de modèle j'utilise un jeu d'essai en dur
        // faudrait créer un modèle dayGantt comme day detail
         NSArray *listeHoraire=[NSArray arrayWithObjects:
                                @"0",@"0",@"2012-12-25 10:00:00",@"2012-12-25 10:30:00",
                                @"1",@"1",@"2012-12-25 10:00:00",@"2012-12-25 12:30:00",
                                @"2",@"0",@"2012-12-25 12:30:00",@"2012-12-25 13:30:00",
                                @"3",@"1",@"2012-12-25 12:30:00",@"2012-12-25 13:00:00",
                                                        nil];
        for (int j=0; j < ([listeHoraire count] +1)/4 ;j++){
           // ganttBandeau *bd = [[ganttBandeau alloc] initWithFrame:CGRectMake(0.0f, HAUTEUR_ENTETE,LARGEUR_COLONNE-5,self.bounds.size.height )];
            ganttBandeau *bd = [[ganttBandeau alloc]
                                initWithDateDeb:[listeHoraire objectAtIndex:(j*4)+2]
                                DateFin:[listeHoraire objectAtIndex:(j*4)+3]
                                ligne:[[listeHoraire objectAtIndex:(j*4)] intValue]
                                type:[[listeHoraire objectAtIndex:(j*4)+1]intValue]
                                ];
            [self addSubview:bd];
            
        }
        
        //
        [self addSubview:_listeEmploye];
        [self addSubview:_listeHoraire];
        
        [self addSubview:self.coinView];
        [self.layer setNeedsDisplay];

        
        
    }

    return self;
}

-(UIView *) getlisteEmploye {
    bandeauGView *res = [[bandeauGView alloc] initWithFrame:CGRectMake(0.0f, HAUTEUR_ENTETE,LARGEUR_COLONNE-5,self.bounds.size.height )];
    return res;
}
-(UIView *) getlisteHoraire {
    enteteView *res = [[enteteView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,LARGEUR_JOUR,HAUTEUR_ENTETE )];
    res.backgroundColor = [UIColor lightGrayColor];
    //[res.layer setDelegate:self];
    NSLog(@"LAYER %@",res.layer.delegate );
   
    
    

    return res;
    
}

///*******
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
    NSLog(@"DELEGATE LAYER");
   // [super drawLayer:layer inContext:ctx];
    UIGraphicsPushContext(ctx);
    //[self setBackgroundColor:[UIColor whiteColor]];
    [[UIColor whiteColor] set];
    UIRectFill(layer.bounds);
    UIGraphicsPopContext();
    
    
    //draw line horaire
    for(int i=0; i < 24; i ++ ){
        //draw line
        //Set the stroke (pen) color
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        //Set the width of the pen mark
        CGContextSetLineWidth(ctx, 1.0);
        // Draw a line
        //Start at this point
        CGContextMoveToPoint(ctx, LARGEUR_COLONNE+(LARGEUR_HEURE*i),HAUTEUR_ENTETE);
        
        //Give instructions to the CGContext
        //(move "pen" around the screen)
        CGContextAddLineToPoint(ctx,LARGEUR_COLONNE+(LARGEUR_HEURE*i),self.bounds.size.height);
        CGContextStrokePath(ctx);
    }
    for(int i=0; i < 24; i ++ ){
        //draw line
        //Set the stroke (pen) color
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        //Set the width of the pen mark
        CGContextSetLineWidth(ctx, 1.0);
        float dashPhase = 0.0;
        float dashLengths[] = {2, 5};
        CGContextSetLineDash(ctx, dashPhase, dashLengths, 2);

        // Draw a line
        //Start at this point
        CGContextMoveToPoint(ctx, LARGEUR_COLONNE+(LARGEUR_HEURE*i)+LARGEUR_HEURE/2,HAUTEUR_ENTETE);
        
        //Give instructions to the CGContext
        //(move "pen" around the screen)
        CGContextAddLineToPoint(ctx,LARGEUR_COLONNE+(LARGEUR_HEURE*i)+LARGEUR_HEURE/2,self.bounds.size.height);
        CGContextStrokePath(ctx);
    }
    
    
    //ligne horizontale
    CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
     CGContextSetLineWidth(ctx, 1.0);
    float dashPhase = 0.0;
    float dashLengths[] = {0, 0};
    CGContextSetLineDash(ctx, dashPhase, dashLengths, 0);
    CGContextMoveToPoint(ctx, 0,HAUTEUR_ENTETE+2);
    
    //Give instructions to the CGContext
    //(move "pen" around the screen)
    CGContextAddLineToPoint(ctx,self.bounds.size.width,HAUTEUR_ENTETE+2);
    CGContextStrokePath(ctx);

    for(int i=1; i < NOMBRE_EMPL+1; i ++ ){
                //ligne en haut
        CGContextMoveToPoint(ctx, 0,(HAUTEUR_LIGNE*i)+HAUTEUR_ENTETE);
        
        //Give instructions to the CGContext
        //(move "pen" around the screen)
        CGContextAddLineToPoint(ctx,self.bounds.size.width,(HAUTEUR_LIGNE*i)+HAUTEUR_ENTETE);
        CGContextStrokePath(ctx);
        
        
        //ligne en bas
        
    }

    
}

// test

-(void) setScroll:(CGPoint) offset{
    
    // ne pas deplacer la liste hoaire sur Y
    CGRect fr = self.listeHoraire.frame;
    fr.origin.y =  offset.y;
    self.listeHoraire.frame  = fr;
    
    //meme choses pour x et employé
    fr = self.listeEmploye.frame;
    fr.origin.x =  offset.x;
    self.listeEmploye.frame=fr;
    //le coin
    fr = self.coinView.frame;
    fr.origin.y = offset.y;
    fr.origin.x =  offset.x;

    self.coinView.frame=fr;

}

//

@end
